package business.command
{
	import business.event.GetUserByUserIdEvent;
	import business.event.LoginEvent;
	import business.service.delegate.BusinessAMDelegate;
	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	import valueObjects.User;
	
	public class LoginCommand implements ICommand, IResponder
	{
		private var isValid:Boolean = false;
		private var loginUser:User;
		public function LoginCommand(){
		}
		public function execute(event:CairngormEvent):void {
			loginUser = (event as LoginEvent).inputUser;
			var businessAM:BusinessAMDelegate = new BusinessAMDelegate(this);
			businessAM.validateUser(loginUser);
		}
		public function result(result:Object):void {
			isValid = result.result as Boolean;
			if(isValid){
				var getUserEvent:GetUserByUserIdEvent = new GetUserByUserIdEvent();
				getUserEvent.userId = loginUser.userId;
				getUserEvent.dispatch();
			}else{
				Alert.show("Wrong user or wrong password ");
			}
		}
		public function fault(info:Object):void {
			
		}
	}
}